#include "gaia_common.h" 
#include "gaia_reply_info.h"
#include "gaia_process_request.h"

/*******************************************
 * 函数名：gaia_reply_info
 * 函数功能：返回任务执行状态
 * 返回值：0
 * 参数说明：connect_fd 创建好的sock句柄
 *           task_id 任务id
 *           status 状态值0-100
 *           msg 返回的消息
 ********************************************/
int gaia_reply_info(int  *connect_fd,int id,int sta,const char *msg)
{

  printf("I'm on function gaia_reply_info!\n");
  char *ret_str = "\r\n\r\n<html><head></head><body>{\"task_id\":\"%d\",\"status\":\"%d\",\"msg\":\"%s\"}</body></html>";
  //char *ret_str = "\r\n\r\n<html><head></head><body>Hello,cassie! </br> task_id:%d,status:%d,msg:%s</body></html>";
  char reply_str[512];
  char message[256];

  int task_id = id;
  int status = sta;
  /*
   * 因为strlen获取到的字符串的真实长度，并未包含结束符'\0',
   * 所以,最后的参数size，必须加1，用于存放字符串结束符'\0'
   */
  strncpy(message,msg,strlen(msg)+1);

  sprintf(reply_str,ret_str,task_id,status,message);

  /* 发送缓冲区设置 */
  unsigned char send_buf[SEND_BUFFER_SIZE + 1]; 

  /* 发送字节数 */
  int send_bytes;

  /* 清空缓冲区内容 */
  memset(send_buf,'\0',sizeof(send_buf));

  send_bytes = strlen(reply_str);
  memcpy(send_buf,reply_str,send_bytes);

  /* 发送数据 */
  send(*connect_fd,send_buf,send_bytes,0);

  /* 断开链接 */
  close(*connect_fd);
  
  return 0;
}
